package com.qf.service.impl;

import com.qf.Resp.BaseResp;
import com.qf.service.HcSearchService;
import com.qf.shop.Shop;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author H
 * @create 2020-11-02 17:44
 * @strive To be whoever you want to be.
 * @Description:
 */
@Service
public class HcSearchServiceImpl implements HcSearchService {


    @Autowired
    RestHighLevelClient restClient;

    @Override
    public BaseResp search(String key) throws IOException, ParseException {
        SearchRequest searchRequest = new SearchRequest("eleme-shop");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        if (key == null || key.equals("null")) {
            searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        } else {
            searchSourceBuilder.query(QueryBuilders.multiMatchQuery(key, "shopName", "shopDesc").field("shopName", 10));
        }

        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font style='color:red'>");
        highlightBuilder.postTags("</font>");
        highlightBuilder.fields().add(new HighlightBuilder.Field("shopName"));
        highlightBuilder.fields().add(new HighlightBuilder.Field("shopDesc"));
        searchSourceBuilder.highlighter(highlightBuilder);

        //加入分页
        int page = 1;
        int size = 5;
        int from = (page - 1) * size;

        searchSourceBuilder.from(from);
        searchSourceBuilder.size(size);

        searchRequest.source(searchSourceBuilder);

        //使用高版本客户执行
        SearchResponse search = restClient.search(searchRequest);

        SearchHits hits = search.getHits();
        long totalHits = hits.getTotalHits();
        SearchHit[] hits1 = hits.getHits();
        List list = new ArrayList<>();

        for (SearchHit hit : hits1
        ) {
            String shopName = null;
            String shopDesc = null;
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();

            if (highlightFields != null) {
                HighlightField name = highlightFields.get("shopName");
                HighlightField desc = highlightFields.get("shopDesc");
                if (name != null) {
                    Text[] fragments = name.getFragments();
                    StringBuffer stringBuffer = new StringBuffer();
                    for (Text text : fragments
                    ) {
                        stringBuffer.append(text);
                    }
                    shopName = stringBuffer.toString();
                }
                if (desc != null) {
                    Text[] fragments = desc.getFragments();
                    StringBuffer stringBuffer = new StringBuffer();
                    for (Text text : fragments
                    ) {
                        stringBuffer.append(text);
                    }
                    shopDesc = stringBuffer.toString();
                }
            }
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            Shop shop = new Shop();
            if (shopName == null) {
                shop.setShopName((String) sourceAsMap.get("shopName"));
            } else {
                shop.setShopName(shopName);
            }
            if (shopDesc == null) {
                shop.setShopDesc((String) sourceAsMap.get("shopDesc"));
            } else {
                shop.setShopDesc(shopDesc);
            }

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            shop.setCreateTime(simpleDateFormat.parse((String) sourceAsMap.get("createtime")));
            shop.setPrice((Double) sourceAsMap.get("price"));
            list.add(shop);
        }
        BaseResp baseResp = new BaseResp();
        baseResp.setMessage(list);
        baseResp.setCode(200l);
        return baseResp;
    }
}

